using System;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;

namespace Code5Systems.FormMaster
{

	public partial class FormMaster : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		
		//Bind the Heading or the Thankyou message
		private void BindData()
		{
			XDocument xDocLocal = default(XDocument);
			XElement xHeading = default(XElement);
			
			if (_objFormCtl.Pages.Count == 1)
			{
				xHeading = this._xDoc.Root.Element("Heading");
				if (xHeading != null)
				{
					if (xHeading.Value != string.Empty)
					{
						TableCell oTableCell = default(TableCell);
						oTableCell = (TableCell) (this.phForm.FindControl("tdFormMasterHeaderCell" + _objFormInfo.CurrentPage.PageNum.ToString()));
						if (oTableCell != null)
						{
							oTableCell.Text = Server.HtmlDecode(xHeading.Value);
						}
					}
				}
			}
			else
			{
				//foreach (Code5Systems.FormMaster.Business.FormPageInfo oPage in _objFormCtl.Pages)
				//{
				//	xDocLocal = XDocument.Parse(oPage.PageData);
				//	xHeading = xDocLocal.Root.Element("Heading");
				//	if (xHeading != null)
				//	{
				//		if (xHeading.Value != string.Empty)
				//		{
				//			TableCell oTableCell = default(TableCell);
				//			oTableCell = (TableCell) (this.formWizard.FindControl("tdFormMasterHeaderCell" + oPage.PageNum.ToString()));
				//			if (oTableCell != null)
				//			{
				//				oTableCell.Text = Server.HtmlDecode(xHeading.Value);
				//			}
				//		}
				//	}
				//}
			}
			
			
			//Me.pnlForm.Visible = True
			this.pnlThankYou.Visible = false;
			
			this.tblThankYou.Width = _objFormSettings.TableWidth;
			
			//RegisterEventHandlers()
			
		}

		private void BindDataRwd()
		{
			XDocument xDocLocal = default(XDocument);
			XElement xHeading = default(XElement);

			if (!String.IsNullOrWhiteSpace(_objFormSettings.TableClass))
			{
				this.pnlForm.CssClass = _objFormInfo.TableClass;
			}

			if (_objFormCtl.Pages.Count == 1)
			{
				xHeading = this._xDoc.Root.Element("Heading");
				if (xHeading != null)
				{
					if (xHeading.Value != string.Empty)
					{
						HtmlGenericControl oHeaderDiv = new HtmlGenericControl("div");
						oHeaderDiv.InnerHtml = Server.HtmlDecode(xHeading.Value);
						this.pnlForm.Controls.AddAt(0, oHeaderDiv);
						//oHeaderDiv = (HtmlGenericControl)(this.FindControl("pnlForm" + _objFormInfo.CurrentPage.PageNum.ToString()));
						//if (oHeaderDiv != null)
						//{
						//	oHeaderDiv.InnerHtml = Server.HtmlDecode(xHeading.Value);
						//}
					}
				}
			}
			else
			{
				//foreach (Code5Systems.FormMaster.Business.FormPageInfo oPage in _objFormCtl.Pages)
				//{
				//	xDocLocal = XDocument.Parse(oPage.PageData);
				//	xHeading = xDocLocal.Root.Element("Heading");
				//	if (xHeading != null)
				//	{
				//		if (xHeading.Value != string.Empty)
				//		{
				//			HtmlGenericControl oHeaderDiv = default(HtmlGenericControl);
				//			oHeaderDiv = (HtmlGenericControl)(this.formWizard.FindControl("tdFormMasterHeaderCell" + oPage.PageNum.ToString()));
				//			if (oHeaderDiv != null)
				//			{
				//				oHeaderDiv.InnerHtml = Server.HtmlDecode(xHeading.Value);
				//			}
				//		}
				//	}
				//}
			}


			//Me.pnlForm.Visible = True
			this.pnlThankYou.Visible = false;

			this.tblThankYou.Width = _objFormSettings.TableWidth;

			//RegisterEventHandlers()

		}
		
		private void RegisterEventHandlers()
		{
			WebControl cmdSubmitButton = null;
			int captchaRowOffset = 0;
			
			Table oFormTable = (Table) (this.pnlForm.FindControl((string) (Utility.GetValidControlID(_objFormInfo.FormName) + _objFormInfo.CurrentPage.PageNum.ToString())));
			
			if (this._objFormCtl.FormButtons.Count() == 0)
			{
				captchaRowOffset = System.Convert.ToInt32(_hasValidation ? 1 : 0);
				WebControl cmdSubmit = null;

				if (this._objFormCtl.Pages.Count > 1 && this._objFormSettings.WizardButtonType != Business.ButtonType.LinkButton)
				{
					if (this._objFormSettings.WizardButtonType == Business.ButtonType.Button)
					{
						Button btn = new Button();
						btn.Text = this._objFormSettings.WizardFinishText;
						btn.ValidationGroup = this._objFormInfo.ValidationGroup;
						btn.Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
						cmdSubmit = btn;
					}
					else
					{
						if (!String.IsNullOrWhiteSpace(_objFormSettings.WizardFinishNextImage))
						{
							int intVal;
							int.TryParse(this._objFormSettings.WizardFinishNextImage, out intVal);
							DotNetNuke.Services.FileSystem.FileInfo oFileInfo = FileManager.Instance.GetFile(intVal) as DotNetNuke.Services.FileSystem.FileInfo;

							ImageButton imgBtn = new ImageButton();
							imgBtn.AlternateText = this._objFormSettings.WizardFinishText;
							imgBtn.ValidationGroup = this._objFormInfo.ValidationGroup;
							imgBtn.Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
							imgBtn.ImageUrl = PortalSettings.HomeDirectory + oFileInfo.Folder + oFileInfo.FileName;
							cmdSubmit = imgBtn;
						}
					}
				}
				else
				{
					LinkButton lb = new LinkButton();
					if (this._objFormCtl.Pages.Count == 1)
						lb.Text = Localization.GetString("cmdSubmit.Text", this.LocalResourceFile);
					else
						lb.Text = this._objFormSettings.WizardFinishText;
					lb.ValidationGroup = this._objFormInfo.ValidationGroup;
					lb.Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
					cmdSubmit = lb;
				}
				cmdSubmit.ID = "cmdSubmit";
				cmdSubmit.CssClass = String.Format("dnnPrimaryAction C5S_cmdSubmit_{0}", this._objFormInfo.FormID);
				TableRow oTableRow = default(TableRow);
				
				int iButtonTabIndex = Code5Systems.FormMaster.Business.FormController.GetControlCount(this._objFormInfo) + 1;
				if (this._objFormInfo.UseCaptcha)
				{
					iButtonTabIndex++;
				}
				cmdSubmit.TabIndex = (short) iButtonTabIndex;
			
				
				TableCell oTableCell = default(TableCell);
				oTableCell = (TableCell) (phForm.FindControl("tdDefaultSubmitButton" + _objFormInfo.CurrentPage.PageNum.ToString()));
				if (oTableCell != null)
				{
					oTableCell.Controls.Add(cmdSubmit);
					oTableRow = (TableRow) (phForm.FindControl("trDefaultSubmitButton" + _objFormInfo.CurrentPage.PageNum.ToString()));
					if (oTableRow != null)
					{
						oTableRow.Visible = true;
						//AddCaptchaRow(oFormTable, cmdSubmit, oFormTable.Rows.Count - captchaRowOffset);
					}
				}
				
				_submitButtons.Add(cmdSubmit);
				
				if (_hasFileUploadControls && DotNetNuke.Framework.AJAX.IsEnabled())
				{
					DotNetNuke.Framework.AJAX.RegisterPostBackControl(cmdSubmit);
				}
			}
			else
			{
				captchaRowOffset += System.Convert.ToInt32(_objFormInfo.ShowCcMe ? 1 : 0);

				foreach (var button in this._objFormCtl.FormButtons)
				{
					switch(button.GetType().Name)
					{
						case "Button":
							((Button)button).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
							break;
						case "LinkButton":
							((LinkButton)button).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
							break;
						case "ImageButton":
							((ImageButton)button).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
							break;
					}

					if (_hasFileUploadControls && DotNetNuke.Framework.AJAX.IsEnabled())
					{
						DotNetNuke.Framework.AJAX.RegisterPostBackControl(button);
					}
				}

				if (this._objFormCtl.FormButtons.Count > 0)
				{
					cmdSubmitButton = this._objFormCtl.FormButtons.Last();
				}

				if (cmdSubmitButton != null)
				{
					this.pnlForm.DefaultButton = cmdSubmitButton.ID;
				}
				
				//for (int iIndex = 0; iIndex <= xSubmitButtons.Count() - 1; iIndex++)
				//{
				//	cmdSubmitButton = (WebControl) (phForm.FindControl((string) (xSubmitButtons[iIndex].Attributes("ID").First().Value)));
				//	if (cmdSubmitButton != null)
				//	{
				//		if ((string) (xSubmitButtons[iIndex].Attributes("ButtonType").First().Value) == "Button")
				//		{
				//			((Button) cmdSubmitButton).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
				//		}
				//		else if ((string) (xSubmitButtons[iIndex].Attributes("ButtonType").First().Value) == "LinkButton")
				//		{
				//			((LinkButton) cmdSubmitButton).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
				//		}
				//		else if ((string) (xSubmitButtons[iIndex].Attributes("ButtonType").First().Value) == "ImageButton")
				//		{
				//			((ImageButton) cmdSubmitButton).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
				//		}
						
				//		if (_hasFileUploadControls && DotNetNuke.Framework.AJAX.IsEnabled())
				//		{
				//			DotNetNuke.Framework.AJAX.RegisterPostBackControl(cmdSubmitButton);
				//		}
						
				//	}
				//}
				
				//Code5Systems.FormMaster.Business.FormControlInfo controlInfo = this._objFormCtl.GetFormControlInfo(cmdSubmitButton.ID);
				//if (controlInfo != null)
				//{
				//	AddCaptchaRow(oFormTable, cmdSubmitButton, (int.Parse(controlInfo.TableRow)) + captchaRowOffset);
				//}

				
			}
		}

		private void RegisterEventHandlersRwd()
		{
			WebControl cmdSubmitButton = null;
			int captchaRowOffset = 0;

			if (this._objFormCtl.FormButtons.Count() == 0)
			{
				captchaRowOffset = System.Convert.ToInt32(_hasValidation ? 1 : 0);
				WebControl cmdSubmit = null;

				if (this._objFormCtl.Pages.Count > 1 && this._objFormSettings.WizardButtonType != Business.ButtonType.LinkButton)
				{
					if (this._objFormSettings.WizardButtonType == Business.ButtonType.Button)
					{
						Button btn = new Button();
						btn.Text = this._objFormSettings.WizardFinishText;
						btn.ValidationGroup = this._objFormInfo.ValidationGroup;
						btn.Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
						cmdSubmit = btn;
					}
					else
					{
						if (!String.IsNullOrWhiteSpace(_objFormSettings.WizardFinishNextImage))
						{
							int intVal;
							int.TryParse(_objFormSettings.WizardFinishNextImage,out intVal);
							DotNetNuke.Services.FileSystem.FileInfo oFileInfo = FileManager.Instance.GetFile(intVal) as DotNetNuke.Services.FileSystem.FileInfo;
							
							ImageButton imgBtn = new ImageButton();
							imgBtn.AlternateText = this._objFormSettings.WizardFinishText;
							imgBtn.ValidationGroup = this._objFormInfo.ValidationGroup;
							imgBtn.Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
							imgBtn.ImageUrl = PortalSettings.HomeDirectory + oFileInfo.Folder + oFileInfo.FileName;
							cmdSubmit = imgBtn;
						}
					}
				}
				else
				{
					LinkButton lb = new LinkButton();
					if (this._objFormCtl.Pages.Count == 1)
						lb.Text = Localization.GetString("cmdSubmit.Text", this.LocalResourceFile);
					else
						lb.Text = this._objFormSettings.WizardFinishText;
					lb.ValidationGroup = this._objFormInfo.ValidationGroup;
					lb.Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
					cmdSubmit = lb;
				}
				cmdSubmit.ID = "cmdSubmit";
				cmdSubmit.CssClass = String.Format("dnnPrimaryAction C5S_cmdSubmit_{0}", this._objFormInfo.FormID);

				int iButtonTabIndex = Code5Systems.FormMaster.Business.FormController.GetControlCount(this._objFormInfo) + 1;
				if (this._objFormInfo.UseCaptcha)
				{
					iButtonTabIndex++;
				}
				cmdSubmit.TabIndex = (short)iButtonTabIndex;

				HtmlGenericControl oRowFluid = default(HtmlGenericControl);
				HtmlGenericControl oCmdDiv = default(HtmlGenericControl);
				oCmdDiv = (HtmlGenericControl)(phForm.FindControl("tdDefaultSubmitButton" + _objFormInfo.CurrentPage.PageNum.ToString()));
				if (oCmdDiv != null)
				{
					oCmdDiv.Controls.Add(cmdSubmit);
					oRowFluid = (HtmlGenericControl)(phForm.FindControl("trDefaultSubmitButton" + _objFormInfo.CurrentPage.PageNum.ToString()));
					if (oRowFluid != null)
					{
						oRowFluid.Visible = true;
					}
				}

				_submitButtons.Add(cmdSubmit);

				if (_hasFileUploadControls && DotNetNuke.Framework.AJAX.IsEnabled())
				{
					DotNetNuke.Framework.AJAX.RegisterPostBackControl(cmdSubmit);
				}
			}
			else
			{
				captchaRowOffset += System.Convert.ToInt32(_objFormInfo.ShowCcMe ? 1 : 0);

				foreach (var button in this._objFormCtl.FormButtons)
				{
					switch(button.GetType().Name)
					{
						case "Button":
							((Button)button).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
							break;
						case "LinkButton":
							((LinkButton)button).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
							break;
						case "ImageButton":
							((ImageButton)button).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
							break;
					}

					if (_hasFileUploadControls && DotNetNuke.Framework.AJAX.IsEnabled())
					{
						DotNetNuke.Framework.AJAX.RegisterPostBackControl(button);
					}
				}

				if (this._objFormCtl.FormButtons.Count > 0)
				{
					cmdSubmitButton = this._objFormCtl.FormButtons.Last();
				}

				if (cmdSubmitButton != null)
				{
					this.pnlForm.DefaultButton = cmdSubmitButton.ID;
				}

				//for (int iIndex = 0; iIndex <= xSubmitButtons.Count() - 1; iIndex++)
				//{
				//	cmdSubmitButton = (WebControl)(phForm.FindControl((string)(xSubmitButtons[iIndex].Attributes("ID").First().Value)));
				//	if (cmdSubmitButton != null)
				//	{
				//		if ((string)(xSubmitButtons[iIndex].Attributes("ButtonType").First().Value) == "Button")
				//		{
				//			((Button)cmdSubmitButton).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
				//		}
				//		else if ((string)(xSubmitButtons[iIndex].Attributes("ButtonType").First().Value) == "LinkButton")
				//		{
				//			((LinkButton)cmdSubmitButton).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
				//		}
				//		else if ((string)(xSubmitButtons[iIndex].Attributes("ButtonType").First().Value) == "ImageButton")
				//		{
				//			((ImageButton)cmdSubmitButton).Command += new System.Web.UI.WebControls.CommandEventHandler(Submit_Click);
				//		}

				//		if (_hasFileUploadControls && DotNetNuke.Framework.AJAX.IsEnabled())
				//		{
				//			DotNetNuke.Framework.AJAX.RegisterPostBackControl(cmdSubmitButton);
				//		}

				//	}
				//}

				//Code5Systems.FormMaster.Business.FormControlInfo controlInfo = this._objFormCtl.GetFormControlInfo(cmdSubmitButton.ID);
				//if (controlInfo != null)
				//{
				//	//AddCaptchaRow(oFormDiv, cmdSubmitButton, (int.Parse(controlInfo.TableRow)) + captchaRowOffset);
				//}
			}
		}
		
	}
}

